package org.leonos.core.config;

import com.jfinal.config.*;
import com.jfinal.log.Log4jLogFactory;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import org.leonos.core.common.Const;
import org.leonos.core.interceptor.ExceptionInterceptor;
import org.leonos.core.orm.BaseModelMapping;
import org.leonos.core.orm.ModulesModelMapping;
import org.leonos.core.route.BaseRoute;
import org.leonos.core.route.ModulesRoute;

/**
 * jfinal-cms 配置文件
 * @author LeonXiao
 */
public class CmsConfig extends JFinalConfig {

    /**
     * 配置常量
     * @param constants
     *              常量
     */
    @Override
    public void configConstant(Constants constants) {
        loadPropertyFile("jdbc.properties");

        constants.setDevMode(getPropertyToBoolean("devMode"));
        constants.setViewType(ViewType.JSP); // 设置视图类型为Jsp，默认为FreeMarker
        constants.setLogFactory(new Log4jLogFactory());

        constants.setErrorView(400, Const.HTTP_PATH + "400.jsp");
        constants.setError401View(Const.HTTP_PATH + "401.jsp");
        constants.setError403View(Const.HTTP_PATH + "403.jsp");
        constants.setError404View(Const.HTTP_PATH + "404.jsp");
        constants.setError500View(Const.HTTP_PATH + "500.jsp");
    }

    /**
     * 配置访问路径
     * @param routes
     *              访问路径
     */
    @Override
    public void configRoute(Routes routes) {
        routes.setBaseViewPath("/WEB-INF/view");
        routes.add(new BaseRoute());
        routes.add(new ModulesRoute());
    }

    /**
     * 配置模板
     * @param engine
     *              模板
     */
    @Override
    public void configEngine(Engine engine) {

    }

    /**
     * 配置插件
     * @param plugins
     *              插件
     */
    @Override
    public void configPlugin(Plugins plugins) {

        // 配置Druid数据库连接配置
        String jdbcUrl = getProperty("jdbcUrl");
        String user = getProperty("user");
        String password = getProperty("password");
        String driverClass = getProperty("driverClass");

        DruidPlugin druid = new DruidPlugin(jdbcUrl, user, password, driverClass);
        druid.setInitialSize(5);
        druid.setMinIdle(5);
        druid.setMaxActive(20);
        druid.setMaxWait(60000);
        druid.setTimeBetweenEvictionRunsMillis(60000);
        druid.setValidationQuery("SELECT 'x'");
        druid.setTestWhileIdle(true);
        druid.setTestOnBorrow(false);
        druid.setTestOnReturn(false);
        druid.setMaxPoolPreparedStatementPerConnectionSize(20);
        druid.setFilters("wall,stat");
        plugins.add(druid);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druid);
        if (getPropertyToBoolean("devMode")) {
            arp.setShowSql(true);
        }
        // 配置ORM映射
        BaseModelMapping.add(arp);
        ModulesModelMapping.add(arp);
        plugins.add(arp);
    }

    /**
     * 配置拦截器
     * @param interceptors
     *              拦截器
     */
    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new ExceptionInterceptor());
    }

    /**
     * 配置处理器
     * @param handlers
     *              处理器
     */
    @Override
    public void configHandler(Handlers handlers) {

    }
}
